package menu.view 
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import utils.T;
	
	/**
	 * ...
	 * @author WLDragon 2014-04-10
	 */
	public class NumberItem extends Sprite 
	{
		
		public function NumberItem(label:String,maxValue:int,len:int,minLen:int) 
		{
			this.maxValue = maxValue;
			this.length = len;
			this.diff = len - minLen;
			this.label = new Bitmap(T.font.getPixelWord(label));
			addChild(this.label);
			
			numbers = [];
			for (var i:int = 0; i < length; i++) 
			{
				var u:Unit = new Unit();
				u.x = 60 + 16 * i;
				if(i<diff)
					u.visible = false;
					
				numbers.push(u);
				addChild(u);
			}
			
			curUnit = numbers[diff];
			_index = diff;
		}
		
		public function active():void
		{
			App.timer.doLoop(200, handleTime);
		}
		
		public function unActive():void
		{
			App.timer.clearTimer(handleTime);
			index = diff;
		}
		
		private function handleTime():void
		{
			curUnit.visible = !curUnit.visible;
		}
		
		public function get value():int 
		{
			return _value;
		}
		
		public function set value(value:int):void 
		{
			if (value < 0 || value > maxValue)
				return;
				
			var s:String = String(value);
			var len:int = s.length;
			for (var i:int = 0; i < length; i++) 
			{
				if (i < (length - len))
				{
					if (i < index)
						numbers[i].visible = false;
						
					numbers[i].update("0");
				}
				else
				{
					numbers[i].update(s.charAt(i + len - length));
					if (i < index)
						numbers[i].visible = true;
				}
			}
			
			_value = value;
		}
		
		public function get index():int 
		{
			return _index;
		}
		
		public function set index(val:int):void 
		{
			if (val < 0 || val > diff)
				return;
			
			if (curUnit)
				curUnit.visible = true;
			
			var dl:int = length - String(value).length;
			for (var i:int = 0; i < dl; i++) 
			{
				if (i < val)
					numbers[i].visible = false;
			}
			
			curUnit = numbers[val];
			_index = val;
		}
		
		/**数字前面的标签*/
		private var label:Bitmap;
		/**数字列表*/
		private var numbers:Array;
		/**位数*/
		public var length:int;
		/**最大长度与能表示的小位数的差值*/
		private var diff:int;
		/**最大能表示的值*/
		private var maxValue:int;
		/**实际值*/
		private var _value:int;
		/**当前激活的数字*/
		private var curUnit:Unit;
		/**第几位数*/
		private var _index:int;
	}

}

import flash.display.Bitmap;
import flash.display.Sprite;
import utils.T;

class Unit extends Sprite
{
	public function Unit()
	{
		bmp = new Bitmap();
		addChild(bmp);
		update("0");
	}
	
	/**
	 * @param	n 0~9
	 */
	public function update(n:String):void
	{
		bmp.bitmapData = T.font.getPixelWord(n);
	}
	
	private var bmp:Bitmap;
}